package ModelLayer;
import java.util.ArrayList;


/**
 * A singleton class to hold a list of orders
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class OrderContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Order> orders;
    private static OrderContainer instance;

    /**
     * Constructor for objects of class OrderContainer
     * Singleton
     */
    private OrderContainer()
    {
        orders = new ArrayList<Order>();
    }
    
    /**
     * Returns an isntance of OrderContainer. If one allready exists 
     * returns that one. Otherwise creates a new one
     */
    public static OrderContainer getInstance()
    {
        if(instance == null)
        {
            instance = new OrderContainer();
        }
        return instance;
    }
    
    /**
     * Adds an order to the list
     */
    public void addOrder(Order order)
    {
        orders.add(order);
    }
    
    /**
     * Removes an order from the list determined by orderID
     */
    public void removeOrder(String orderID)
    {
        Order removeOrder = getOrder(orderID);
        orders.remove(removeOrder);
    }
    
    /**
     * Returns an order from the list found by orderID
     */
    public Order getOrder(String orderID)
    {
        boolean found = false;
        int i = 0;
        Order searchOrder = null;
        while(!found && i < orders.size())
        {
            if(orders.get(i).getOrderID().equals(orderID))
            {
                searchOrder = orders.get(i);
                found = true;
            }
            else
            {
                i++;
            }
        }
        return searchOrder;
    }
}